package br.com.intelsys.basics.dao.hiber;

import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.intelsys.basics.dao.Trans;

public class TransHiber implements Trans {
	private final Transaction transaction;
	private final Session session;

	TransHiber(final DaoHiber daoHiber) {
		session = daoHiber.getLocalSession();
		transaction = session.beginTransaction();
	}

	public void saveOrUpdate(Object entity) {
		if (entity != null) {
			session.saveOrUpdate(entity);
		}
	}

	public void saveOrUpdateAndAfterRefresh(Object entity, Object... refreshObject) {
		saveOrUpdate(entity);
		refreshList(refreshObject);
	}

	public void delete(Object entity) {
		if (entity != null) {
			session.delete(entity);
		}
	}

	public void deleteAfterRefresh(Object entity, Object... refreshObject) {
		delete(entity);
		refreshList(refreshObject);
	}

	public void commit() {
		transaction.commit();
	}

	public void rollback() {
		transaction.rollback();
	}

	public Trans beginTransaction() {
		transaction.begin();
		return this;
	}

	public void refresh(Object entity) {
		if (entity != null) {
			session.refresh(entity);
		}
	}

	public void refreshList(Object... entitys) {
		if (entitys != null) {
			for (Object entity : entitys) {
				refresh(entity);
			}
		}
	}

}
